/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.mailbox.Color;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.ArrayUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.AutoSendDraftTask;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.util.TagUtil;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.service.FileUploadServlet;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.IOException;
/*     */ import java.util.Arrays;
/*     */ import java.util.Date;
/*     */ import java.util.Map;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SaveDraft
/*     */   extends MailDocumentHandler
/*     */ {
/*  54 */   private static final String[] TARGET_DRAFT_PATH = { "m", "id" };
/*  55 */   private static final String[] TARGET_FOLDER_PATH = { "m", "l" };
/*  56 */   private static final String[] RESPONSE_ITEM_PATH = new String[0];
/*     */   
/*     */   protected String[] getProxiedIdPath(Element request)
/*     */   {
/*  60 */     return getXPath(request, TARGET_DRAFT_PATH) != null ? TARGET_DRAFT_PATH : TARGET_FOLDER_PATH;
/*     */   }
/*     */   
/*     */   protected boolean checkMountpointProxy(Element request)
/*     */   {
/*  65 */     return getXPath(request, TARGET_DRAFT_PATH) == null;
/*     */   }
/*     */   
/*     */   protected String[] getResponseItemPath() {
/*  69 */     return RESPONSE_ITEM_PATH;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException {
/*  73 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  74 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  75 */     OperationContext octxt = getOperationContext(zsc, context);
/*  76 */     ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
/*     */     
/*  78 */     Element msgElem = request.getElement("m");
/*     */     
/*  80 */     int id = (int)msgElem.getAttributeLong("id", -1L);
/*  81 */     String originalId = msgElem.getAttribute("origid", null);
/*  82 */     ItemId iidOrigid = originalId == null ? null : new ItemId(originalId, zsc);
/*  83 */     String replyType = msgElem.getAttribute("rt", null);
/*  84 */     String identity = msgElem.getAttribute("idnt", null);
/*  85 */     String account = msgElem.getAttribute("forAcct", null);
/*     */     
/*     */ 
/*  88 */     String folderId = msgElem.getAttribute("l", null);
/*  89 */     ItemId iidFolder = new ItemId(folderId == null ? "-1" : folderId, zsc);
/*  90 */     if (!iidFolder.belongsTo(mbox))
/*  91 */       throw ServiceException.INVALID_REQUEST("cannot move item between mailboxes", null);
/*  92 */     if ((folderId != null) && (iidFolder.getId() <= 0)) {
/*  93 */       throw MailServiceException.NO_SUCH_FOLDER(iidFolder.getId());
/*     */     }
/*  95 */     String flags = msgElem.getAttribute("f", null);
/*  96 */     String[] tags = TagUtil.parseTags(msgElem, mbox, octxt);
/*  97 */     Color color = ItemAction.getColor(msgElem);
/*     */     
/*     */ 
/* 100 */     String attachment = msgElem.getAttribute("aid", null);
/* 101 */     long autoSendTime = new Long(msgElem.getAttribute("autoSendTime", "0")).longValue();
/*     */     
/* 103 */     ParseMimeMessage.MimeMessageData mimeData = new ParseMimeMessage.MimeMessageData();
/*     */     Message msg;
/*     */     try { MimeMessage mm;
/*     */       MimeMessage mm;
/* 107 */       if (attachment != null) {
/* 108 */         mm = SendMsg.parseUploadedMessage(zsc, attachment, mimeData);
/*     */       } else {
/* 110 */         mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, mbox, msgElem, null, mimeData);
/*     */       }
/*     */       
/* 113 */       long date = System.currentTimeMillis();
/*     */       try {
/* 115 */         Date d = new Date();
/* 116 */         mm.setSentDate(d);
/* 117 */         date = d.getTime();
/*     */       }
/*     */       catch (Exception ignored) {}
/*     */       try {
/* 121 */         mm.saveChanges();
/*     */       } catch (MessagingException me) {
/* 123 */         throw ServiceException.FAILURE("completing MIME message object", me);
/*     */       }
/*     */       
/* 126 */       ParsedMessage pm = new ParsedMessage(mm, date, mbox.attachmentsIndexingEnabled());
/*     */       
/* 128 */       if (autoSendTime != 0L) {
/* 129 */         AccountUtil.checkQuotaWhenSendMail(mbox);
/*     */       }
/*     */       
/* 132 */       String origid = iidOrigid == null ? null : iidOrigid.toString(account == null ? mbox.getAccountId() : account);
/*     */       
/* 134 */       msg = mbox.saveDraft(octxt, pm, id, origid, replyType, identity, account, autoSendTime);
/*     */     } catch (IOException e) {
/* 136 */       throw ServiceException.FAILURE("IOException while saving draft", e);
/*     */     }
/*     */     finally {
/* 139 */       if (mimeData.fetches != null) {
/* 140 */         FileUploadServlet.deleteUploads(mimeData.fetches);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 145 */     if (mimeData.uploads != null) {
/* 146 */       FileUploadServlet.deleteUploads(mimeData.uploads);
/*     */     }
/*     */     
/*     */ 
/* 150 */     if ((folderId != null) || (flags != null) || (!ArrayUtil.isEmpty(tags)) || (color != null)) {
/*     */       try
/*     */       {
/* 153 */         ItemActionHelper.UPDATE(octxt, mbox, zsc.getResponseProtocol(), Arrays.asList(new Integer[] { Integer.valueOf(msg.getId()) }), MailItem.Type.MESSAGE, null, null, iidFolder, flags, tags, color);
/*     */         
/*     */ 
/* 156 */         msg = mbox.getMessageById(octxt, msg.getId());
/*     */       } catch (ServiceException e) {
/* 158 */         ZimbraLog.soap.warn("error setting metadata for draft " + msg.getId() + "; skipping that operation", e);
/*     */       }
/*     */     }
/*     */     
/* 162 */     if (schedulesAutoSendTask()) {
/* 163 */       if (id != -1)
/*     */       {
/* 165 */         AutoSendDraftTask.cancelTask(id, mbox.getId());
/*     */       }
/* 167 */       if (autoSendTime != 0L)
/*     */       {
/* 169 */         AutoSendDraftTask.scheduleTask(msg.getId(), mbox.getId(), autoSendTime);
/*     */       }
/*     */     }
/*     */     
/* 173 */     return generateResponse(zsc, ifmt, octxt, mbox, msg);
/*     */   }
/*     */   
/*     */   protected boolean schedulesAutoSendTask() {
/* 177 */     return true;
/*     */   }
/*     */   
/*     */   protected Element generateResponse(ZimbraSoapContext zsc, ItemIdFormatter ifmt, OperationContext octxt, Mailbox mbox, Message msg) {
/* 181 */     Element response = zsc.createElement(MailConstants.SAVE_DRAFT_RESPONSE);
/*     */     try {
/* 183 */       ToXML.encodeMessageAsMP(response, ifmt, octxt, msg, null, -1, true, true, null, true, false, false);
/*     */     } catch (MailServiceException.NoSuchItemException nsie) {
/* 185 */       ZimbraLog.soap.info("draft was deleted while serializing response; omitting <m> from response");
/*     */     } catch (ServiceException e) {
/* 187 */       ZimbraLog.soap.warn("problem serializing draft structure to response", e);
/*     */     }
/* 189 */     return response;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/SaveDraft.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */